package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczAzClf;
import com.punai.dataanalysis.epdatakb.mapper.JtczAzClfMapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.system.mapper.EcoCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JtczAzClfService {
    @Autowired
    private JtczAzClfMapper azClfMapper;
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;


    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        List<JtczAzClf> resultList = new ArrayList<>();
        Map<String, Object> yearMonth = dataAnalysisVo.getYearMonth();
        List<JtczResultBo> wccz = azClfMapper.wccz(yearMonth);
        Map<String, BigDecimal> wcczMap = wccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> zc = azClfMapper.zc(yearMonth);
        Map<String, BigDecimal> zcMap = zc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> fc = azClfMapper.fc(yearMonth);
        Map<String, BigDecimal> fcMap = fc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        
        
        /**
         * 专业 - 路桥 公司
         */
        List<EcoCompany> ecoCompanies = ecoCompanyMapper.selectList(Wrappers.<EcoCompany>lambdaQuery().eq(EcoCompany::getType, "Z")
                .eq(EcoCompany::getSource, "A")
                .eq(EcoCompany::getStatus,"0")
        );

        if (CollUtil.isEmpty(ecoCompanies)) {
            throw new ServiceException("无统计单位!");
        }

        List<String> companyNames = ecoCompanies.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());

        for (String companyName : companyNames) {
            JtczAzClf azClf = new JtczAzClf();
            azClf.setDeptName(companyName);
            azClf.setBnwccz(wcczMap.getOrDefault(companyName,BigDecimal.ZERO));
            azClf.setZc(zcMap.getOrDefault(companyName,BigDecimal.ZERO));
            azClf.setFc(fcMap.getOrDefault(companyName,BigDecimal.ZERO));
            azClf.setClfhj(NumberUtil.adds(
                    azClf.getZc(),
                    azClf.getFc()
            ));
            calcZb(azClf);
            resultList.add(azClf);
        }
        calcPm(resultList);
        JtczAzClf xj = new JtczAzClf();
        xj.setDeptName("安装单位小计");
        calcXj(resultList,xj);
        resultList.add(xj);
        return resultList;
    }

    private void calcPm(List<JtczAzClf> resultList) {
        resultList.sort(Comparator.comparing(JtczAzClf::getZczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczAzClf azClf = resultList.get(i);
            azClf.setZcpm(i + 1);
        }
        resultList.sort(Comparator.comparing(JtczAzClf::getFczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczAzClf azClf = resultList.get(i);
            azClf.setFcpm(i + 1);
        }
 
        resultList.sort(Comparator.comparing(JtczAzClf::getZb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczAzClf azClf = resultList.get(i);
            azClf.setPm(i + 1);
        }
    }

    private void calcZb(JtczAzClf azClf) {
        BigDecimal bnwccz = azClf.getBnwccz();
        BigDecimal zc = azClf.getZc();
        BigDecimal fc = azClf.getFc();
        BigDecimal clfhj = azClf.getClfhj();
        azClf.setZczb(NumberUtil.divideUseProportion(zc,2,bnwccz));
        azClf.setFczb(NumberUtil.divideUseProportion(fc,2,bnwccz));
        azClf.setZb(NumberUtil.divideUseProportion(clfhj,2,bnwccz));
    }

    private void calcXj(List<JtczAzClf> resultList,JtczAzClf xj) {
        BigDecimal bnwccz = BigDecimal.ZERO;
        BigDecimal zc     = BigDecimal.ZERO;
        BigDecimal fc     = BigDecimal.ZERO;
        BigDecimal clfhj  = BigDecimal.ZERO;

        for (JtczAzClf azClf : resultList) {
            bnwccz  = NumberUtil.add(bnwccz ,azClf.getBnwccz());
            zc      = NumberUtil.add(zc     ,azClf.getZc());
            fc      = NumberUtil.add(fc     ,azClf.getFc());
            clfhj   = NumberUtil.add(clfhj  ,azClf.getClfhj());
        }

        xj.setBnwccz( bnwccz  );
        xj.setZc(     zc      );
        xj.setFc(     fc      );
        xj.setClfhj(  clfhj   );

        calcZb(xj);
    }


}
